﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ActionServices;
using CoreData;
using Helper;
namespace MMO50K
{
    public class BaseController : Controller
    {
        public int UserID;
        public string UploadPath;
        public static List<Permission> PermissionUser { get; set; }
        public static string UserName { get; set; }
        public readonly IusersActionService UserService;
        public readonly IuserinfoActionService UserInforService;
        public readonly Iuser_groupActionService UserGroupService;
        public readonly IjobActionService JobService;
        public readonly IworkActionService WorkService;

        public BaseController(IuserinfoActionService UserInfor)
        {
            this.UserInforService = UserInfor; 
        }
        public BaseController(Iuser_groupActionService UserGroup)
        {
            this.UserGroupService = UserGroup;   
        }
        public BaseController(IusersActionService User, Iuser_groupActionService usergroup,
            IuserinfoActionService userinfo)
        {
            this.UserService = User;
            this.UserGroupService = usergroup;
            this.UserInforService = userinfo;
        }
        public BaseController(IworkActionService work) {
            this.WorkService = work;
        }

        public BaseController(IjobActionService job) {
            this.JobService = job;
        }
 
        /// <summary>
        /// Login method
        /// </summary>
        /// <param name="_model">Get _model (username & password) from view to check for Login</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login(users _model) {
            IList<vwusers> data;
            bool IsLogin =UserService.Login(_model.username, _model.password, out data);
            if(IsLogin)
            {
                return RedirectToAction("Index", "Home");
            }
            else
            {
               return ErrorView(ErrorCode.LoginFailed);
            }
        }

        /// <summary>
        /// Register Method
        /// </summary>
        /// <param name="_model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Register(users _model)
        {
            try
            {
                _model.active = true;
                _model.createdate = DateTime.Now;
                _model.modifieddate = DateTime.Now;
            
                string error = null;//
                long save = this.UserService.Save(_model, out error);
                if(error!=null)
                {
                    return ErrorView(error);
                }
                if (save != -1)
                {

                    user_group usergroupmodel = new user_group();
                    usergroupmodel.userid =save;
                    usergroupmodel.groupid = 1;
                    this.UserGroupService.Save(usergroupmodel);
                    userinfo uinfor = new userinfo();
                    uinfor.userid = save;
                    uinfor.username = _model.username;
                    this.UserInforService.Save(_model);
                    return RedirectToAction("Login", "Authurization");
                }
                else
                {
                    return ErrorViewModel(ErrorCode.Error, _model);
                }
            }
            catch {
                return ErrorViewModel(ErrorCode.Error, _model);
            }
        }

        public ActionResult Logout() {
            UserService.Logout();
            return RedirectToAction("Login", "Authurization");
        }

        #region Method

        ViewResult ErrorView(string error)
        {
            ModelState.AddModelError("Error", error);
            return View();
        }
        ViewResult ErrorViewModel(string error, object model)
        {
            ModelState.AddModelError("Error", error);
            return View(model);
        }
        #endregion

    }
}
